package com.ado.abyss.core.manager.impl;

import com.ado.abyss.core.dal.dao.SysLogMapper;
import com.ado.abyss.core.dal.model.SysLog;
import com.ado.abyss.core.manager.SysLogManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author adonais
 * @date 2020/7/9
 * @description SysLogManagerImpl
 **/
@Service(value = "sysLogManager")
public class SysLogManagerImpl extends ServiceImpl<SysLogMapper , SysLog> implements SysLogManager {

    @Autowired
    private SysLogMapper sysLogMapper ;

    @Override
    public List<SysLog> findAll() {
        return sysLogMapper.findAll();
    }

    @Override
    public SysLog findById(Long id) {
        return sysLogMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public boolean deleteById(Long id) {
        boolean flag = false ;
        int result = sysLogMapper.deleteByPrimaryKey(id) ;
        if(result == 1) {
            flag = true ;
        }
        return flag;
    }

    @Override
    @Transactional
    public boolean saveLogBatch(List<SysLog> sysLogs) {
        boolean flag = false ;
        int count = 0 ; // 计数 统计插入成功的次数，若次数等于集合长度，则批量插入结果为true，否则为false
        for (SysLog sysLog : sysLogs) {
            int result = sysLogMapper.insertSelective(sysLog) ;
            if (result == 1) {
                count++;
            }
        }
        if (sysLogs.size()+1 == count) {
            flag = true ;
        }
        return flag;
    }

    @Override
    @Transactional
    public boolean saveLog(SysLog sysLog) {
        boolean flag = false ;
        int result = sysLogMapper.insertSelective(sysLog) ;
        if (result == 1) {
            flag = true ;
        }
        return flag;
    }
}
